package com.miyu.system.service.sys.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.pojo.cos.CosDict;
import com.miyu.common.utils.commonUtils.CommonUtils;
import com.miyu.system.dao.sys.CosDictDao;
import com.miyu.system.service.sys.CosDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统dict服务impl
 *
 * @author MIYU
 * @date 2022/06/05
 */
@Service
public class CosDictServiceImpl implements CosDictService {

    @Resource
    private CosDictDao dictDao;

    /**
     * 编辑系统dict
     *
     * @param param 参数
     * @return {@code JSONObject}
     */
    @Override
    public JSONObject editCosDict(JSONObject param) {

        JSONObject dictTable= param.getJSONObject("dictTable");
        CosDict dict = new CosDict();
        if(dictTable.containsKey("dictId")){
            dict.setDictId(dictTable.getString("dictId"));
        }else{
            String dictId = CommonUtils.getUUID();
            dict.setDictId(dictId);
        }

        JSONArray dictColum = param.getJSONArray("dictColum");
        dict.setDictTable(dictTable.getString("dictTable"));
        dict.setDictColumns(dictColum.toString());
        dict.setDictMark(dictTable.getString("dictMark"));
        dict.setDictName(dictTable.getString("dictName"));
        dict.setDictType(dictTable.getString("dictType"));
        dictDao.saveOrUpdateByNotNullProperties(dict);
        JSONObject rs = new JSONObject();
        rs.put("code", "1");
        rs.put("msg", "操作成功");
        return rs;
    }

    /**
     * 查询系统dict
     *
     * @param param 参数
     * @return {@code JSONArray}
     */
    @Override
    public JSONArray queryCosDict(JSONObject param) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select *  from Cos_Dict where 1=1 ");
        JSONArray rs =  dictDao.queryJSONArrayBySql(sql.toString());
        System.out.println(rs);
        return rs;
    }

    /**
     * 查询dict列
     *
     * @param param 参数
     * @return {@code JSONArray}
     */
    @Override
    public JSONArray queryDictColumns(JSONObject param) {
        CosDict dict = JSONObject.toJavaObject(param, CosDict.class);
        String Columns = dict.getDictColumns();
        JSONArray rs = JSONArray.parseArray(Columns);
        JSONArray cells= new JSONArray();
        for(int i=0;i<rs.size();i++){
            JSONObject cel = new  JSONObject();
            JSONObject col = rs.getJSONObject(i);
            cel.put("key",CommonUtils.caseHump(col.getString("columCode")));
            cel.put("title",col.getString("columName"));
            cells.add(cel);
        }

        return cells;
    }

    /**
     * 得到表数据
     *
     * @param param 参数
     * @return {@code JSONArray}
     */
    @Override
    public JSONArray getTableData(JSONObject param) {
        String tableName = param.getString("dictTable");
        StringBuffer sql = new StringBuffer();
        sql.append(" select *  from ").append(tableName);
        JSONArray rs = dictDao.queryJSONArrayBySql(sql.toString());
        System.out.println(rs);
        return rs;
    }

    /**
     * 删除因为dict
     *
     * @param param 参数
     * @return {@code JSONObject}
     */
    @Override
    public JSONObject delCosDict(JSONArray param) {
        List<CosDict> list = JSONArray.parseArray(param.toString(), CosDict.class);
        dictDao.deleteAll(list);
        JSONObject rs = new JSONObject();
        rs.put("code", "1");
        rs.put("msg", "操作成功");
        return rs;
    }
}
